window.tempGenerate={
	animate:[]
};
tempGenerate.init = function(relation,fdom,iidata){
	//获取数据后依据页码重新组装数据
	var infDatas = [];
	tempGenerate.animate = [];
	for (let infname in iidata) {
		iidata[infname].forEach(function(item,index){
			item["itype"] = infname;
		})
		infDatas = infDatas.concat(iidata[infname]);
	}
	//依据页码排序
	infDatas.sort(function (a, b) {
		if (a.ym < b.ym) {
			return -1;
		} else if (a.ym == b.ym) {
			return 0;
		} else {
			return 1;
		}
	});
	//通过加载数据重新组装 dom结构化数据
	infDatas.forEach(function(item,index){
		console.log(item);
		var section = $(`<div class="section"></div>`);
		section.appendTo(fdom);
		//得到组装数据后的eles
		var eles = relation[item.itype];
		eles = tempGenerate.domAddData(eles,item);
		//当为嘉宾页的时候数据做特殊处理 加上嘉宾数据
		if(item.itype == "infinitusInvitationGuest"){
			var iigdatas = item.datas;
			if(iigdatas.length == 1){
				var childs = tempGenerate["iigDataDom"].one;
				tempGenerate.addChildData(childs,iigdatas[0]);
				eles = eles.concat(childs);
			}
			if(iigdatas.length == 2){
				var left1 = tempGenerate["iigDataDom"].left1;
				tempGenerate.addChildData(left1,iigdatas[0]);
				eles = eles.concat(left1);
				var right = tempGenerate["iigDataDom"].right;
				tempGenerate.addChildData(right,iigdatas[1]);
				eles = eles.concat(right);
			}
			if(iigdatas.length == 3){
				var left1 = tempGenerate["iigDataDom"].left1;
				tempGenerate.addChildData(left1,iigdatas[0]);
				eles = eles.concat(left1);
				var right = tempGenerate["iigDataDom"].right;
				tempGenerate.addChildData(right,iigdatas[1]);
				eles = eles.concat(right);
				var left2 = tempGenerate["iigDataDom"].left2;
				tempGenerate.addChildData(childs,iigdatas[2]);
				eles = eles.concat(left2);
			}
			//datas
		}
		//得到组装数据后的dom数组然后开始渲染生成dom结构
		//设置每一页的动画集合,然后封装到动画库中等待执行
		var domAnimate = [];
		tempGenerate.initdom(eles,section,domAnimate);
		tempGenerate.animate.push(domAnimate);
		
		
		
	})
}
tempGenerate.addChildData = function(childs = [],data){
	childs.forEach(function(item){
		if(item.data){
			if(item.type == "img")item.iname = data[item.data.name];
			else item.text = data[item.data.name];
		}
	});
}
tempGenerate.domAddData = function(eles,infData){
	var newEles = [];
	eles.forEach(function(item){
		//将item的style/3
		if(item.data){
			if(item.data.type == "list"){
				var dlist = infData[item.data.name];
				if(dlist && dlist.length > 0){
					tempGenerate.addChildData(item.childs,dlist[0]);
					newEles.push(item);
					for(var i=1;i<dlist.length;i++){
						//得到一个新的对象 开始新增
						var copyItem = JSON.parse(JSON.stringify(item));
						copyItem.name = item.name+i;
						copyItem.style.top = (parseInt(item.style.top) + (i*item.data.lin))+"px";
						tempGenerate.addChildData(copyItem.childs,dlist[i]);
						//设置动画延迟时间
						copyItem.childs.forEach(function(item2){
							if(item2.animate){
								item2.animate.delay = 150*i;
							}
						});
						newEles.push(copyItem);
					}
				}
			}else{
				if(item.type == "img")item.iname = infData[item.data.name];
				else item.text = infData[item.data.name];
				newEles.push(item);
			}
		}else if((!item.data || item.data.type != "list") && item.childs){
			item.childs = tempGenerate.domAddData(item.childs,infData)
			newEles.push(item);
		}else{
			newEles.push(item);
		}
	});
	return newEles;
}
tempGenerate.initdom = function(eles,pdom,domAnimate){
	eles.forEach(function(item,index){
		var ele;
		if(item.type == "img"){
			ele = $(`<div class="ele" name="${item.name}"  style="background-image: url(${item.iname})"></div>`);
			if(item.childs){
				tempGenerate.initdom(item.childs,ele,domAnimate);
			}
		}
		if(item.type == "gif"){
			ele = $(`<img src="${item.iname}"  alt="" srcset="">`);
		}
		if(item.type == "text"){
			ele = $(`<div class="ele" name="${item.name}" >${item.text}</div>`);
		}
		if(item.type == "div"){
			ele = $(`<div class="ele" name="${item.name}" ></div>`);
			if(item.childs){
				tempGenerate.initdom(item.childs,ele,domAnimate);
			}
		}
		if(item.type == "textarea"){
			ele = $(`<textarea class="ele" name="${item.name}" >${item.text}</textarea>`);
		}
		ele.css(item.style);
		domAnimate.push({
			dom:ele,
			animate:item.animate,
			animateClass:item.animateClass
		});
		ele.appendTo(pdom);
	});
}
/**
 * 擦除效果
 * 1.linear	2. swing	3. easeInQuad	4. easeOutQuad	5. easeInOutQuad	6. easeInCubic
					
7. easeOutCubic	8. easeInOutCubic	9. easeInQuart	10. easeOutQuart	11. easeInOutQuart	12. easeInQuint
					
13. easeOutQuint	14. easeInOutQuint	15. easeInExpo	16. easeOutExpo	17. easeInOutExpo	18. easeInSine
					
19. easeOutSine	20. easeInOutSine	21. easeInCirc	22. easeOutCirc	23. easeInOutCirc	24. easeInElastic
					
25. easeOutElastic	26. easeInOutElastic	27. easeInBack	28. easeOutBack	29. easeInOutBack	30. easeInBounce
		 	 	 	 
31. easeOutBounce	32. easeInOutBounce
 */
tempGenerate.options={
	easing:"easeOutQuad",//设置全局擦除效果
	speed:1500,//设置全局动画执行速度
	delay:0,//设置全局延迟时间
}
tempGenerate.animateReady = function(index){
	var as = tempGenerate.animate[index-1];
	as.forEach(function(item){
		var dom = item.dom,a=item.animate;
		var animateClass=item.animateClass || [];
		dom.stop();
		for(var i=0;i<animateClass.length;i++){
			dom.removeClass(animateClass[i]);
		}
		if(a){
			dom.css(a.in);
		}
		
	})
}
//开始执行动画库
tempGenerate.startAnimate = function(index,type){
	type = type || "to";
	var as = tempGenerate.animate[index-1];
	as.forEach(function(item){
		var dom = item.dom,a=item.animate;
		var animateClass=item.animateClass || [];
		for(var i=0;i<animateClass.length;i++){
			dom.addClass(animateClass[i]);
		}
		if(a){
			dom.delay(a.delay||tempGenerate.options.delay).animate(a[type],{
			  duration: a.speed ||tempGenerate.options.speed,
			  easing: a.easing||tempGenerate.options.easing
			});
		}
	})
}
function loadJS( url, callback ){
	var script = document.createElement('script'),
		fn = callback || function(){};
	script.type = 'text/javascript';
	//IE
	if(script.readyState){
		script.onreadystatechange = function(){
			if( script.readyState == 'loaded' || script.readyState == 'complete' ){
				script.onreadystatechange = null;
				fn();
			}
		};
	}else{
		script.onload = function(){
			fn();
		};
	}
	script.src = url;
	document.getElementsByTagName('head')[0].appendChild(script);
}
function getQueryVariable(variable)
{
	   var query = window.location.search.substring(1);
	   var vars = query.split("&");
	   for (var i=0;i<vars.length;i++) {
			   var pair = vars[i].split("=");
			   if(pair[0] == variable){return pair[1];}
	   }
	   return(false);
}
